import UIKit
import AssetsLibrary

private let albumCellID = "albumCell"
private let cellRowHeight: CGFloat = 78

// cell默认颜色: Boder, 填充颜色
private let normalColor = UIColor(red: 78.0 / 255.0, green: 183.0 / 255.0, blue: 186.0 / 225.0, alpha: 1.0)

class AlbumTableViewController: UIViewController {
    
    var leftButtonTitle: String = kLeftButtonTitle
    var pickerControllerTitle: String = kPickerControllerTitle
    var rightButtonTitle: String = kRightButtonTitle
    // MARK: - 开放的属性
    var maxSelectedCount = Int.max
    var perRowNumber = kNumberOfImagePerRow
    // 选择图片最大 10M
    var maxByteSize = kMaxByteSize
    // cell 选中边框颜色
    var borderColor = normalColor
    // cell 选中边上角label 填充颜色
    var fillLabelColor = normalColor
    weak var pickerDelegate: ImageContentViewControllerDelegate?
    // 未授权
    var authorizationAlertView: UIAlertView?
    // 选择超出限定
    var maxSelectedAlertView: UIAlertView?
    // 照片大小过大
    var maxByteSizeAlertView: UIAlertView?
    // 相册类型
    var groupType = ALAssetsGroupSavedPhotos
    // 选择的样式 类型
    var type: ImagePickerType = .Default
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.rowHeight = cellRowHeight
        tableView.separatorStyle = .None
        tableView.dataSource = self
        tableView.delegate = self
        tableView.registerClass(AlbumCell.self, forCellReuseIdentifier: albumCellID)
        tableView.frame = mainScreenBounds
        
        var top: CGFloat = CGRectGetMaxY(UIApplication.sharedApplication().statusBarFrame)
        if let navigationController = self.navigationController {
            top = top + CGRectGetMaxY(navigationController.navigationBar.frame)
        }
        tableView.contentInset = UIEdgeInsets(top: top, left: 0, bottom: 0, right: 0)
        
        return tableView
    }()
    
    // 导航栏左侧按钮
    private lazy var leftBarButtonItem: UIBarButtonItem = {
        let leftBarButtonItem = UIBarButtonItem(title: self.leftButtonTitle, style: .Plain, target: self, action: #selector(ImageContentViewController.didTapCancel))
        return leftBarButtonItem
    }()
    
    private var albums: [Album] = []
    private var imagePickerHelp: ImagePickerHelp?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        automaticallyAdjustsScrollViewInsets = false
        navigationItem.leftBarButtonItem = leftBarButtonItem
        view.addSubview(tableView)
        
        imagePickerHelp = ImagePickerHelp(type: ALAssetsGroupAll, filter: ALAssetsFilter.allPhotos()) { (status, albums) in
            self.albums = albums
            self.tableView.reloadData()
        }
        
        pushImageContentVC(ALAssetsGroupSavedPhotos, animated: false)
    }
    
    func pushImageContentVC(groupType: UInt32, animated: Bool) {
        let contentVC = ImageContentViewController()
        contentVC.pickerDelegate = pickerDelegate
        contentVC.borderColor = borderColor
        contentVC.fillLabelColor = fillLabelColor
        contentVC.maxSelectedCount = maxSelectedCount
        contentVC.leftButtonTitle = leftButtonTitle
        contentVC.title = pickerControllerTitle
        contentVC.rightButtonTitle = rightButtonTitle
        contentVC.groupType = groupType
        contentVC.perRowNumber = perRowNumber
        contentVC.type = type
        
        if let authorizationAlertView = authorizationAlertView {
            contentVC.authorizationAlertView = authorizationAlertView
        }
        
        if let maxSelectedAlertView = maxSelectedAlertView {
            contentVC.maxSelectedAlertView = maxSelectedAlertView
        }
        
        if let maxByteSizeAlertView = maxByteSizeAlertView {
            contentVC.maxByteSizeAlertView = maxByteSizeAlertView
        }
        
        navigationController?.pushViewController(contentVC, animated: animated)
    }
    
    func didTapCancel() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    deinit {
        print("AlbumTableViewController: deinit")
    }
}

extension AlbumTableViewController: UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return albums.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(albumCellID) as! AlbumCell
        let album = albums[indexPath.row]
        cell.album = album
        return cell
    }
}

extension AlbumTableViewController: UITableViewDelegate {
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let album = albums[indexPath.row]
        pushImageContentVC(album.type, animated: true)
    }
}
